import java.util.Arrays;

public class MyArrayList {
    private int[] elem;
    private int usedSize;
    public MyArrayList() {
        this.elem = new int[10];
    }

    //头插
    public void addHead(int data) {
        if(isFull()) {
            grow();
        }
        for (int i = usedSize-1; i >= 0; i--) {
            elem[i+1] = elem[i];
        }
        elem[0] = data;
        usedSize++;
    }

    //尾插
    public void addLast(int data) {
        if(isFull()) {
            grow();
        }
        elem[usedSize] = data;
        usedSize++;
    }
    //在pos下标位置，插入元素data
    public void add(int pos, int data) {
        if(isFull()) {
            grow();
        }
        if(pos < 0 || pos > usedSize) {
            throw new PosOutOfException("pos位置不合法");
        }
        for (int i = usedSize-1; i >= pos; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    //删除pos下标位置的元素
    public void remove(int pos) {
        if(isEmpty()) {
            throw new EmptyArrayListException("顺序表为空");
        }
        for (int i = pos; i < usedSize; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    //判断数组是否为空
    public boolean isEmpty() {
        return usedSize == 0;
    }

    //判断数组是否满了
    public boolean isFull() {
        return usedSize == elem.length;
    }

    //如果数组满了，进行扩容
    public void grow() {
        elem = Arrays.copyOf(elem,2*elem.length);
    }

    //打印顺序表
    public void print() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
    }
}
